package com.ficus.road.maintain.core.utils;

import com.ficus.road.maintain.core.configs.SmartDecisionConfig;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * @author xiawen yang
 * @date 2022/8/19 下午2:59
 */
@Component
@RequiredArgsConstructor
@Data
public class SmartDecisionUtils implements ApplicationContextAware {
    private static ApplicationContext applicationContext;

    public static double convertHighwaySmartTableType2Weight(String type) {
        SmartDecisionConfig smartDecisionConfig = getBean(SmartDecisionConfig.class);
        SmartDecisionConfig.HighwayConfig highwayConfig = smartDecisionConfig.getHighwayConfig();
        double res;
        switch (type) {
            case "第一大类":
                res = highwayConfig.getType1Weight();
                break;
            case "第二大类":
                res = highwayConfig.getType2Weight();
                break;
            case "第三大类":
                res = highwayConfig.getType3Weight();
                break;
            case "第四大类":
                res = highwayConfig.getType4Weight();
                break;
            case "第五大类":
                res = highwayConfig.getType5Weight();
                break;
            case "储备库":
            default:
                res = highwayConfig.getType6Weight();
                break;
        }
        return res;
    }

    public static <T> T getBean(Class<T> clazz) {
        if (applicationContext == null) {
            throw new RuntimeException("applicationContext获取失败");
        }
        return applicationContext.getBean(clazz);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SmartDecisionUtils.applicationContext = applicationContext;
    }
}
